using System;
using System.Data;
using DayPilot.Utils;
using DayPilot.Web.Ui.Enums;

public partial class Scheduler_CssStyling : System.Web.UI.Page
{
    private DataTable table;

    protected void Page_Load(object sender, EventArgs e)
    {
        #region Data loading initialization
        if (Session["CssStyling"] == null)
        {
            Session["CssStyling"] = DataGenerator.GetData();
        }
        table = (DataTable)Session["CssStyling"];
        DayPilotScheduler1.DataSource = Session["CssStyling"];
        #endregion


        if (!IsPostBack)
        {
            DayPilotScheduler1.StartDate = Week.FirstDayOfWeek();
            DataBind();
        }

    }

    protected void DayPilotScheduler1_EventMove(object sender, DayPilot.Web.Ui.Events.EventMoveEventArgs e)
    {
        #region Simulation of database update

        DataRow dr = table.Rows.Find(e.Value);
        if (dr != null)
        {
            dr["start"] = e.NewStart;
            dr["end"] = e.NewEnd;
            dr["column"] = e.NewResource;
            table.AcceptChanges();
        }

        #endregion

        DayPilotScheduler1.DataBind();
        DayPilotScheduler1.Update();
    }
    protected void DayPilotScheduler1_EventResize(object sender, DayPilot.Web.Ui.Events.EventResizeEventArgs e)
    {
        #region Simulation of database update

        DataRow dr = table.Rows.Find(e.Value);
        if (dr != null)
        {
            dr["start"] = e.NewStart;
            dr["end"] = e.NewEnd;
            table.AcceptChanges();
        }

        #endregion

        DayPilotScheduler1.DataBind();
        DayPilotScheduler1.Update();
    }

    protected void DayPilotScheduler1_TimeRangeSelected(object sender, DayPilot.Web.Ui.Events.TimeRangeSelectedEventArgs e)
    {
        #region Simulation of database update

        DataRow dr = table.NewRow();
        dr["start"] = e.Start;
        dr["end"] = e.End;
        dr["id"] = Guid.NewGuid().ToString();
        dr["name"] = "New event";
        dr["column"] = e.ColumnId;

        table.Rows.Add(dr);
        table.AcceptChanges();
        #endregion

        DayPilotScheduler1.DataBind();
        DayPilotScheduler1.Update();
    }

    protected void DayPilotScheduler1_Refresh(object sender, DayPilot.Web.Ui.Events.RefreshEventArgs e)
    {
        DayPilotScheduler1.StartDate = e.StartDate;
        DayPilotScheduler1.DataBind();
        DayPilotScheduler1.Update();
    }

    protected void DayPilotScheduler1_Command(object sender, DayPilot.Web.Ui.Events.CommandEventArgs e)
    {
        switch (e.Command)
        {
            case "previous":
                DayPilotScheduler1.StartDate = DayPilotScheduler1.StartDate.AddDays(-7);
                break;
            case "next":
                DayPilotScheduler1.StartDate = DayPilotScheduler1.StartDate.AddDays(7);
                break;
            case "today":
                DayPilotScheduler1.StartDate = Week.FirstDayOfWeek();
                break;
        }

        DayPilotScheduler1.DataBind();
        DayPilotScheduler1.Update(CallBackUpdateType.Full);
    }

}
